package com.sk.bilibili.view.fragment.search;

import android.util.Log;

import androidx.viewbinding.ViewBinding;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.pager.search.SearchFragmentAdapter;
import com.sk.bilibili.adapter.vlayout.BaseCollectionAdapter;
import com.sk.bilibili.adapter.vlayout.videoSet.introduction.RecommendAdapter;
import com.sk.bilibili.model.fragment.search.BaseSearchModel;
import com.sk.bilibili.view.fragment.BaseRefreshFragment;

import java.util.List;

public abstract class BaseSearchFragment<M extends BaseSearchModel,D> extends BaseRefreshFragment<M> {

    protected SearchFragmentAdapter parent;
    protected BaseCollectionAdapter<?extends ViewBinding,D> collectionAdapter;
    protected int pos;

    public BaseSearchFragment(SearchFragmentAdapter parent, int pos) {
        this.parent = parent;
        this.pos = pos;
    }

    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        collectionAdapter=getAdapter();
        adapter.addAdapter(collectionAdapter);
    }

    protected abstract BaseCollectionAdapter<? extends ViewBinding, D> getAdapter();

    @Override
    public void onRefresh() {
        model.refreshData(parent.getKeyword());
    }

    public void setData(List<D> list){
        collectionAdapter.setData(list);//该清空还是要清空
        if(list.isEmpty()){
            toast("没有数据请换个关键词搜索");
        }
        cancelRefresh();
    }

    public void addData(List<D> list){
        if(list.isEmpty()){
            toast("没有更多了");
        }else {
            collectionAdapter.addData(list);
        }
    }

    public void setNum(Integer num){
        parent.setNum(pos,num);
    }

}